<script setup>
import { useUserStoreHook } from "@/store/modules/user";
import { useRouter } from "vue-router";
import { ref, computed, h } from "vue";

import { ReText } from "@/components/ReText";
import Profile from "./components/Profile.vue";
import { useRenderIcon } from "@/components/ReIcon/src/hooks";
import { useGlobal, deviceDetection } from "@pureadmin/utils";
import LaySidebarTopCollapse from "@/layout/components/lay-sidebar/components/SidebarTopCollapse.vue";
import MemberDialog from "@/components/MemberDialog/index.vue";
import { addDialog, closeDialog } from "@/components/ReDialog";

import leftLine from "@iconify-icons/ri/arrow-left-s-line";
import timer from "@/assets/svg/timer.svg?component";
import cloud from "@/assets/svg/cloud.svg?component";
import member from "@/assets/svg/member.svg?component";
import account from "@/assets/svg/account.svg?component";
import help from "@/assets/svg/help.svg?component";

import TransactionHistory from "./components/TransactionHistory.vue";
defineOptions({
  name: "AccountSettings"
});

const router = useRouter();
let optionsName = ref("去开通");
optionsName.value =
  useUserStoreHook().roleTag == "NORMAL" ? "去开通" : "去续费";
console.log(useUserStoreHook());

const isOpen = ref(deviceDetection() ? false : true);

const witchPane = ref("profile");

const panes = [
  {
    key: "profile",
    label: "账号设置",
    icon: account,
    component: Profile
  },
  {
    key: "TransactionHistory",
    label: "交易记录",
    icon: timer,
    component: TransactionHistory
  },
  {
    key: "buyMember",
    label: "会员购买",
    icon: member,
    component: ""
  },
  {
    key: "helpCenter",
    label: "会员权益",
    icon: help,
    component: ""
  },
  {
    key: "myCloud",
    label: "我的云端",
    icon: cloud,
    component: ""
  }
];
const handleChangeNav = item => {
  if (deviceDetection()) {
    isOpen.value = !isOpen.value;
  }
  if (!item.component && item.key !== "buyMember") {
    router.push({ name: item.key });
    return;
  }
  if (item.key == "buyMember") {
    handleBuyMember();
    return;
  }
  witchPane.value = item.key;
};
const handleBuyMember = () => {
  addDialog({
    width: "30%",
    hideFooter: true,
    class: "restDialog",
    showClose: true,
    contentRenderer: () => h(MemberDialog)
  });
};

const backgroundImageStyle = computed(() => {
  const bgUrl =
    useUserStoreHook().roleTag === "NORMAL"
      ? new URL("/normal.svg", import.meta.url).href
      : new URL("/member.svg", import.meta.url).href;
  return {
    backgroundImage: `url(${bgUrl})`
  };
});
</script>

<template>
  <el-container class="h-full">
    <el-aside
      v-if="isOpen"
      class="acount-settings overflow-hidden px-2 border-r-[1px]"
      :width="deviceDetection() ? '180px' : '400px'"
    >
      <div class="acount-settings-menu">
        <div class="text-[15px] cursor-pointer" @click="router.go(-1)">
          <div class="flex items-center">
            <IconifyIconOffline :icon="leftLine" class="text-[20px]" />
            <span>返回</span>
          </div>
        </div>
        <div class="user-info">
          <!-- 用户信息 -->
          <div class="member-card userbg" :style="backgroundImageStyle">
            <p class="member-card-title">{{ useUserStoreHook().roleName }}</p>
            <p
              v-if="useUserStoreHook().roleTag === 'NORMAL'"
              class="member-card-desc"
            >
              会员升级享更多权益
            </p>
            <p v-else class="member-card-desc">
              {{ useUserStoreHook().vipEndTime }}
            </p>
            <div class="member-card-user">
              <div class="flex items-center">
                <el-avatar :size="30" :src="useUserStoreHook().avatar" />
                <span class="ml-2">{{ useUserStoreHook().nickname }}</span>
              </div>
              <ReText
                type="primary"
                class="cursor-pointer"
                @click="handleBuyMember"
                >{{ optionsName }}</ReText
              >
            </div>
          </div>
          <!-- 导航信息 -->
          <div class="nav">
            <p>快速访问</p>
            <div class="nav-content">
              <div
                v-for="item in panes"
                :key="item.key"
                class="nav-item flex items-center"
                :class="witchPane === item.key ? 'active' : ''"
                @click="handleChangeNav(item)"
              >
                <div class="flex items-center z-10">
                  <component
                    :is="useRenderIcon(item.icon)"
                    class="w-[20px] h-[20px] mr-3"
                  />
                  <span>{{ item.label }}</span>
                </div>
              </div>
            </div>
          </div>

          <!-- 内存空间 -->
          <div class="spatial">
            <p>内存空间</p>
            <div class="flex items-center justify-center">
              <el-progress
                type="circle"
                :indeterminate="true"
                :percentage="
                  parseInt(
                    (useUserStoreHook().quantityUsed /
                      useUserStoreHook().quantity) *
                      100
                  )
                "
                color="#6a32c9"
                :width="220"
                :stroke-width="25"
              />
            </div>
          </div>
          <!-- 提示信息 -->
          <div class="flex justify-center">
            <ReText type="primary">{{
              `当前容量为${useUserStoreHook().quantity}个模板, 您可以通过升级Vip来扩充您的容量`
            }}</ReText>
          </div>
        </div>
      </div>
    </el-aside>
    <el-main>
      <LaySidebarTopCollapse
        v-if="deviceDetection()"
        class="px-0"
        :is-active="isOpen"
        @toggleClick="isOpen = !isOpen"
      />
      <component
        :is="panes.find(item => item.key === witchPane).component"
        :class="[!deviceDetection() && 'ml-[30px]']"
      />
    </el-main>
  </el-container>
</template>

<style lang="scss">
.acount-settings-menu {
  padding: 20px 10px;
  background-color: transparent;
  border: none;
}

.restDialog {
  padding: 0;

  .el-dialog__header {
    padding: 0;
  }
}
</style>

<style lang="scss" scoped>
body[layout] {
  .el-menu--vertical .is-active {
    color: #fff !important;
    transition: color 0.2s;

    &:hover {
      color: #fff !important;
    }
  }
}

.user-info {
  padding: 10px;

  .member-card {
    position: relative;
    height: 200px;
    padding: 25px 15px;
    border-radius: 10px;

    &-title {
      font-size: 22px;
      font-weight: 900;
      color: #5b5f63;
    }

    &-desc {
      margin-top: 5px;
      font-size: 12px;
      color: #686b6e;
    }

    &-user {
      position: absolute;
      bottom: 25px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      width: 90%;
      font-size: 13px;
    }
  }

  .nav {
    padding: 20px 0;
    margin: 20px 0;
    border: 1px solid #eef2f9;
    border-right: 0;
    border-left: 0;

    p {
      margin-bottom: 10px;
      font-size: 18px;
      font-weight: 700;
      color: #5b5f63;
    }

    .nav-content {
      display: flex;
      flex-wrap: wrap;
      align-items: center;

      .nav-item {
        width: 50%;
        padding: 10px 0;
        font-size: 17px;
        cursor: pointer;
        transition: all 0.3s;
      }
    }
  }

  .spatial {
    padding: 20px 0;

    p {
      width: 100%;
      margin-bottom: 30px;
      font-size: 18px;
      font-weight: 700;
      color: #5b5f63;
    }
  }
}

.userbg {
  background-repeat: no-repeat;
  background-position: center;
  background-size: 100% 100%;
}

.active {
  font-weight: 600;
  color: #4081cc;
}
</style>
